<?php
/**
 * Created by PhpStorm.
 * userValidate: hms
 * Date: 2018/8/7
 * Time: 9:27
 */

namespace app\mini\controller;

use think\Controller;
use think\Config;
use redis\Redis;
use think\Db;
use think\Request;
use think\Session;
use app\mini\model\DataUser;
use app\mini\model\Member;
use app\index\model\Order;
use app\mini\model\Getcash;
use app\mini\model\Recharge;
use app\mini\validate\userValidate;
use app\Tools\VerifyCodeImage;

class Dataofuser extends Controller

{
    const PERPAGE = 10;



    /**
     * 登录
     * @param Request $request
     * @param app\api\model\User $user
     * @return \think\response\Json
     * @author cpy(1442435897@qq.com)
     */
    public function login(Request $request, DataUser $user) {
        $param = $request->param();
        $redis = Redis::instance();
//        if (Session::has('code')) {
//            $code = Session::get('code');
//            if (strtolower($code) == strtolower(trim($param['code']))) {
                if ($param['mobile'] && $param['password']) {
                    $where['mobile'] = ['eq', trim($param['mobile'])];
                    $user_info = $user->getUserInfo($where);
                    if ($user_info) {
                        $password = sha1(md5(trim($param['password'].$user_info['salt'])));
                        if ($password == $user_info['password']) {
                            Db::startTrans();
                            try {
                                $token = accessToken();
                                if (!empty($user_info['token']))
                                    $redis->hDel('data_user', $user_info['front_token']);
                                $user_info['login_time'] = time();
                                $user_info['front_token'] = $token;
                                $redis->hSet('data_user', $token, json_encode($user_info));
                                $redis->set('data_user_info:' . $user_info['id'], $token);
                                Session('user_id', $user_info['id']);
                                $user->save(['token' => $token], ['mobile' => $param['mobile']]);
//                                Session::delete('code');
                                Db::commit();
                                $result = Config::get('code.success');
                                $result['token'] = $token;
                                $result['mobile'] = $user_info['mobile'];
                            } catch (\Exception $e) {
                                Db::rollback();
                                return $e->getMessage();
                            }
                        } else {
                            $result['msg'] = '密码错误';
                            $result['status'] = 203;
                        }
                    } else {
                        $result['msg'] = '该手机号未注册';
                        $result['status'] = 203;
                    }
                } else {
                    $result = Config::set('code.mistake');
                }
//            } else {
//                $result = Config::get('code.codeFail');
//            }
//        } else {
//            $result = Config::get('code.codeEmpty');
//        }
        return json($result);
    }

    /*
     * 获取图形验证码
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return json
     */
    public function getPicCode()
    {
        $code = createRandomNum(4);
        Session('code',$code);
        $imageCode = new VerifyCodeImage(80,20,$code);
        $file = $imageCode->show();
        $result = Config::get('code.success');
        $result['code'] = $file;
        return json($result);
    }

    /**
     * 退出
     * @param Request $request
     * @return \think\response\Json
     * @author cpy(1442435897@qq.com)
     */
    public function loginout(Request $request)
    {
        $user_info = $request->data_user;
        $redis = Redis::instance();
        $res = $redis->hDel('user_info', $user_info['token']);
        if($res){
            Session::delete('user_id');
            $result = Config::get('code.success');
        }else{
            $result = Config::get('code.error');
        }
        return json($result);
    }

    /**
     * 主页
     * @param Request $request
     * @return \think\response\Json
     * @author cpy(1442435897@qq.com)
     */
    public function index(Request $request, Order $order, Member $member, Recharge $recharge,Getcash $getcash)
    {
        $where['status'] = ['gt',1];
        $where['type'] = ['eq',1];
        $where['project_id'] = ['not in',[36,38,39,40]];
        $orderInfo = $order->selectInfo($where,'sum(money)');
        $where['create_time'] = ['between time',[date('Y-m-d'),date("Y-m-d",strtotime("+1 day"))]];
        $orderDay = $order->selectInfo($where,'sum(money)');

        $mwhere['is_disable'] = ['eq',1];
        $allUser = $member->selectAllInfo($mwhere, 'id');
        $mwhere['create_time'] = ['between time',[date('Y-m-d'),date("Y-m-d",strtotime("+1 day"))]];
        $dayLogin = $member->selectAllInfo($mwhere, 'id');
        $mwhere['is_check'] = ['eq',1];
        $dayAuth = $member->selectAllInfo($mwhere, 'id');
        $authUser = $member->selectAllInfo(['is_disable' => 1,'is_check' => 1], 'id');
        $allMoney = $member->selectAllInfo('', 'sum(money)');

        $rwhere['status'] = ['eq',2];
        $rwhere['date'] = ['between time',[date('Y-m-d'),date("Y-m-d",strtotime("+1 day"))]];
        $rechargeInfo = $recharge->selectInfo($rwhere,'sum(money)');
        $getcashInfo = $getcash->selectInfo($rwhere,'sum(money)');

        $result = Config::get('code.success');
        $result['allMoney'] = empty($orderInfo[0]['sum(money)'])?0:$orderInfo[0]['sum(money)'];
        $result['dayMoney'] = empty($orderDay[0]['sum(money)'])?0:$orderDay[0]['sum(money)'];
        $result['alluser'] = count($allUser);
        $result['authUser'] = count($authUser);
        $result['allMemberMoney'] = empty($allMoney[0]['sum(money)'])?0:$allMoney[0]['sum(money)'];
        $result['dayLogin'] = count($dayLogin);
        $result['dayAuth'] = count($dayAuth);
        $result['recharge'] = empty($rechargeInfo[0]['sum(money)'])?0:$rechargeInfo[0]['sum(money)'];
        $result['getcash'] = empty($getcashInfo[0]['sum(money)'])?0:$getcashInfo[0]['sum(money)'];
        return json($result);
    }


    /**
     * 总募资明细
     * @param Request $request
     * @return \think\response\Json
     * @author  hey_heyang(635870453@qq.com)
     */
    public function getTotalList(Request $request, Order $order)
    {
        $param = $request->param();
        if (empty($param['curpage'])) {
            $curpage = 1;
        } else {
            $curpage = $param['curpage'];
        }
        $dataList = $order->selectAllinvest();
        $total_count = count($dataList);
        $total_page = ceil($total_count/self::PERPAGE);
        $data_list = pageArray($dataList,self::PERPAGE,$curpage);
        outPutData(array('list'=>$data_list,'total_page'=>$total_page,'total_count'=>$total_count));

    }


    /**
     * 获取今日投资明细
     * @param Request $request
     * @return \think\response\Json
     * @author  hey_heyang(635870453@qq.com)
     */
    public function getTodayOrder(Request $request, Order $order)
    {
        $param = $request->param();
        if (empty($param['curpage'])) {
            $curpage = 1;
        } else {
            $curpage = $param['curpage'];
        }
        $dataList = $order->getTodayOrderList($param);
        $total_count = count($dataList['res']);
        $total_page = ceil($total_count/self::PERPAGE);
        $data_list = pageArray($dataList['res'],self::PERPAGE,$curpage);
        outPutData(array('list'=>$data_list,'total_money'=>$dataList['total'],'total_page'=>$total_page,'total_count'=>$total_count));

    }

    /**
     * 会员充值明细
     * @param Request $request
     * @return \think\response\Json
     * @author  hey_heyang(635870453@qq.com)
     */

    public function getMemberRechargeList(Request $request, Recharge $Recharge)
    {
        $param = $request->param();
        if (empty($param['curpage'])) {
            $curpage = 1;
        } else {
            $curpage = $param['curpage'];
        }
        $dataList = $Recharge->getRechargeList($param['time_begin'],$param['time_end']);
        $total_count = count($dataList['res']);
        $total_page = ceil($total_count/self::PERPAGE);
        $data_list = pageArray($dataList['res'],self::PERPAGE,$curpage);
        outPutData(array('list'=>$data_list,'total_money'=>$dataList['total'],'total_page'=>$total_page,'total_count'=>$total_count));

    }

    /**
     * 会员提现明细
     * @param Request $request
     * @return \think\response\Json
     * @author  hey_heyang(635870453@qq.com)
     */

    public function getMemberGetcashList(Request $request, Getcash $Getcash)
    {
        $param = $request->param();
        if (empty($param['curpage'])) {
            $curpage = 1;
        } else {
            $curpage = $param['curpage'];
        }
        $dataList = $Getcash->getCashList($param['time_begin'],$param['time_end']);
        $total_count = count($dataList['res']);
        $total_page = ceil($total_count/self::PERPAGE);
        $data_list = pageArray($dataList['res'],self::PERPAGE,$curpage);
        outPutData(array('list'=>$data_list,'total_money'=>$dataList['total'],'total_page'=>$total_page,'total_count'=>$total_count));

    }

}